<?php
class FieldValidator {
    
    public static function validateCodPostal($codPostal = "") {
        return preg_match("/^[1-9]\d{3}-\d{3}$/", $codPostal);
    }
    
    public static function validateTelefone($telefone = ""){
        return preg_match("/^[29]\d{8}$/", $telefone);
    }
    
    public static function validateEmail($email = ""){
        return preg_match("/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/", $email);
    }
    
    public static function validateNumber($number = ""){
        return preg_match("/^\d+$/", $number);
    }
    
    public static function validateDate($date = ""){
        $nums = array();
        if(!preg_match("/^(\d{4})-(\d\d)-(\d\d)$/", $date, $nums)){
            return false;
        }
        return checkdate($nums[2], $nums[3], $nums[1]);
    }
    
    public static function validateAvatar($file = null){
        if(!$file){
            return false;
        }
        if($file['error'] == 0){
            $info = getimagesize($file['tmp_name']);
            $valid = $info[0] <= 50 && $info[1] <=  50;
            $valid = $valid && ($info[2] == IMAGETYPE_GIF || $info[2] == IMAGETYPE_JPEG || $info[2] == IMAGETYPE_PNG);
        }
        
        return $valid;
        
    }
    
    public static function converterData($data){
            preg_match("/^(\d\d)-(\d\d)-(\d{4})$/", $date, $nums);
            return $nums[2]."-".$nums[1]."-".$nums[3];
    }
    
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
